package com.csdj.controller;

import com.alibaba.fastjson.JSON;
import com.csdj.entity.Account;
import com.csdj.entity.Page;
import com.csdj.exception.BussinessException;
import com.csdj.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;


    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }

    /**
     * 登录
     * @param cardNo
     * @param passWord
     * @param request
     * @return
     */
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public String doLogin(String cardNo, String passWord, HttpServletRequest request){
        boolean isLogin = accountService.login(cardNo, passWord);
        HttpSession session = request.getSession();
        System.out.println("强");
        if (isLogin){
            session.setAttribute("cardNo",cardNo);
        }

        return JSON.toJSONString(isLogin);
    }


    @RequestMapping("/transfer")
    public String toTransfer(){
        return "tranferAccount";
    }

    @RequestMapping("/check")
    @ResponseBody
    public String check(String cardNo){
        Account account = accountService.getAccount(cardNo);
        return JSON.toJSONString(account);
    }

    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    @RequestMapping("/seeBalance")
    public String getBalance(HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        String cardNo =session.getAttribute("cardNo").toString();
        double balance = accountService.seeBanlance(cardNo);
        model.addAttribute("balance",balance);
        return "seeBalance";
    }

    @RequestMapping("/doTransfer")
    @ResponseBody
    public String doTransfer(String toCardNo,String money,HttpServletRequest request) throws BussinessException {
        HttpSession session = request.getSession();
        String cardNo =session.getAttribute("cardNo").toString();

        boolean isTransfer = accountService.transfer(cardNo, toCardNo, Double.parseDouble(money));
        return JSON.toJSONString(isTransfer);
    }

    @RequestMapping("/list")
    public ModelAndView page(@RequestParam(value = "starDate",required = false) String starDate, @RequestParam(value = "endDate",required = false)String endDate, Integer curPageNo){
        int pageSize = 3;
        if(curPageNo==null || curPageNo==0){
            curPageNo=1;
        }
        ModelAndView mv = new ModelAndView();
        Page page = accountService.findRecord(starDate, endDate, curPageNo, pageSize);
        mv.addObject("page",page);
        mv.addObject("starDate",starDate);
        mv.addObject("endDate",endDate);
        mv.setViewName("list");
        return mv;
    }
}
